package com.atguigu.lease.web.app.controller.login;



import com.atguigu.lease.common.context.LoginUser;
import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.web.app.service.LoginService;
import com.atguigu.lease.web.app.service.SmsService;
import com.atguigu.lease.web.app.service.UserInfoService;
import com.atguigu.lease.web.app.vo.user.LoginVo;
import com.atguigu.lease.web.app.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Tag(name = "登录管理")
@RequestMapping("/app/")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private UserInfoService userInfoService;

    // 获取短信验证码操作完成
    @GetMapping("login/getCode")
    @Operation(summary = "获取短信验证码")
    public Result getCode(@RequestParam String phone) {
        loginService.getCode(phone);
        return Result.ok();
    }

    // 登录模块完成
    @PostMapping("login")
    @Operation(summary = "登录")
    public Result<String> login(@RequestBody LoginVo loginVo) {
        // 因为是登录 所以就是登录以后获取创建的token
        String token =  loginService.login(loginVo);
        return Result.ok(token);
    }


    // 获取登录用户信息 一样也是配置拦截器操作
    @GetMapping("info")
    @Operation(summary = "获取登录用户信息")
    public Result<UserInfoVo> info() {
        // 通过本地线程中读取到登录用户信息
        LoginUser loginUser = LoginUserContext.getLoginUser();
        // 再通过用户信息中的id去数据库查找对应的用户记录
        UserInfoVo userInfoVo =  userInfoService.info(loginUser.getUserId());
        return Result.ok(userInfoVo);
    }
}
